<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Datagram
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Abstract.php 7 2009-11-14 22:42:08Z nkeyes $
 */


/**
 * 
 * @author Nathan Keyes
 *
 */
abstract class Bushido_Datagram_Abstract extends Bushido_Object implements Bushido_Configurable_Interface
{
	protected $chartName = NULL;
	protected $rawData = NULL;
	protected $allowedConfigOptions;
	protected $config;
	
	
	/**
	 * 
	 * @param array $configOptions
	 * @return void
	 */
	public function __construct($configOptions = array())
	{
		$this->_setRawData();
	}
	
	/**
	 * 
	 * @return void
	 */
	protected abstract function _setRawData();
	
	
	/**
	 * 
	 * @return array
	 */
	public function getRawData()
	{
		if(!isset($this->rawData))
		{
			$this->_setRawData();
			/*
			if(!isset($this->rawData))
			{
				$this->rawData = array(array
				(
					'name' => $this->getChartDisplayName(),
					'data' => array
					(
						array
						(
							'label' => date("m/Y"),
							'value' => 100
						)
					)
				));
			}
			*/
		}
		return $this->rawData;
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getChartName()
	{
		if($this->chartName === NULL)
		{
			$this->chartName = get_class($this);
		}
		return $this->chartName;
	}
	/**
	 * 
	 * @return string
	 */
	public abstract function getChartDisplayName();
	
	
	/**
	 * 
	 * @param $seriesName
	 * @param $resultSet
	 * @return void
	 */
	protected function _buildSeriesRawData($seriesName, $resultSet)
	{
		$seriesData = array();
		
		if(is_array($resultSet))
		{
			foreach($resultSet as $label => $value)
			{
				$seriesData[] = array('label' => $label, 'value' => $value);
			}
			unset($label);
			unset($value);
		}
		$series = array('name' =>  $seriesName, 'data' => $seriesData);
		return $series;	
	}
	
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Configurable/Bushido_Configurable_Interface#getConfig()
	 */
	public function getConfig()
	{
		return $this->config;
	}
	
	/**
	 * 
	 * @param $seriesName
	 * @param $resultSet
	 * @return void
	 */
	public function setSeries($seriesName, $resultSet)
	{
		$newSeries = $this->_buildSeriesRawData($seriesName, $resultSet);
		
		if(!$this->rawData)
		{
			$this->rawData = array();
			$this->rawData[] = $newSeries;
			return;
		}
		if($this->getSeries($seriesName))
		{
			$this->deleteSeries($seriesName);
		}
		$this->rawData[] = $newSeries;
		
	}
	
	
	/**
	 * 
	 * @param $seriesName
	 * @return array
	 */
	public function getSeries($seriesName)
	{
		if(is_array($this->rawData))
		{
			foreach($this->rawData as $series)
			{
				if(isset($series['name']) && $series['name'] == $seriesName)
				{
					return $series;
				}
			}
			unset($series);
		}
		return null;
	}
	
	/**
	 * 
	 * @param $seriesName
	 * @return array
	 */
	public function getAllSeries()
	{
		return $this->getRawData();
	}
	
	
	/**
	 * 
	 * @param $seriesName
	 * @return void
	 */
	public function deleteSeries($seriesName)
	{
		foreach($this->rawData as $seriesIndex => $series)
		{
			if(isset($series['name']) && $series['name'] == $seriesName)
			{
				unset($this->rawData[$seriesIndex]);
			}
		}
		unset($seriesIndex);
		unset($series);
	}
	
	/**
	 * 
	 * @param $seriesName
	 * @param $dataPointName
	 * @param $dataPointValue
	 * @return void
	 */
	public function setSeriesDataPoint($seriesName, $dataPointName, $dataPointValue)
	{
		$series = $this->getSeries($seriesName);
		if(!$series)
		{
			$this->setSeries($seriesName, array($dataPointName => $dataPointValue));
			return;
		}
		foreach($series['data'] as $dataPointIndex => $dataPoint)
		{
			if(isset($dataPoint['label']) && $dataPoint['label'] == $dataPointName)
			{
				$series['data'][$dataPointIndex]['value'] = $dataPointValue;
				break;
			}
		}
		unset($dataPointIndex);
		unset($dataPoint);
		$series['data'][] = array('label' => $dataPointName, 'value' => $dataPointValue);
		$this->setSeries($seriesName, $this->_seriesToResultSet($series));
		return;
	}
	
	/**
	 * 
	 * @param $seriesName
	 * @param $dataPointName
	 * @return array
	 */
	public function getSeriesDataPoint($seriesName, $dataPointName)
	{
		$series = $this->getSeries($seriesName);
		if(!$series)
		{
			return null;
		}
		
		foreach($series['data'] as $dataPoint)
		{
			if(isset($dataPoint['label']) && $dataPoint['label'] == $dataPointName)
			{
				return $dataPoint;
			}
		}
		unset($dataPoint);
		return null;
	}
	
	/**
	 * 
	 * @param $series
	 * @return void
	 */
	protected function _seriesToResultSet($series)
	{
		$resultSet = array();
		foreach($series['data'] as $dataPoint)
		{
			$resultSet[$dataPoint['label']] = $dataPoint['value'];
		}
		unset($dataPoint);
		return $resultSet;
	}
}